#!/bin/bash

echo "🔧 创建模拟构建产物来验证流程..."

# 创建临时测试项目
mkdir -p mock-build-test
cd mock-build-test

# 创建简化的package.json
cat > package.json << 'EOF'
{
  "name": "mock-build-test",
  "version": "1.0.0",
  "scripts": {
    "build:mp-weixin": "node create-mock-files.js"
  }
}
EOF

# 创建模拟文件生成脚本
cat > create-mock-files.js << 'EOF'
const fs = require('fs');
const path = require('path');

console.log('🎭 生成模拟的微信小程序文件...');

// 创建目录结构
const dirs = [
  'dist/build/mp-weixin',
  'dist/build/mp-weixin/pages',
  'dist/build/mp-weixin/pages/index'
];

dirs.forEach(dir => {
  fs.mkdirSync(dir, { recursive: true });
  console.log(`📁 创建目录: ${dir}`);
});

// 创建模拟的微信小程序文件
const files = [
  {
    path: 'dist/build/mp-weixin/app.js',
    content: `// 模拟的app.js文件
App({
  onLaunch: function () {
    console.log('App Launch')
  }
})`
  },
  {
    path: 'dist/build/mp-weixin/app.json',
    content: JSON.stringify({
      "pages": ["pages/index/index"],
      "window": {
        "navigationBarTitleText": "测试小程序"
      }
    }, null, 2)
  },
  {
    path: 'dist/build/mp-weixin/app.wxss',
    content: `/* 模拟的全局样式 */
.container {
  padding: 20rpx;
}`
  },
  {
    path: 'dist/build/mp-weixin/pages/index/index.js',
    content: `// 模拟的页面JS
Page({
  data: {
    message: 'Hello UniApp!'
  }
})`
  },
  {
    path: 'dist/build/mp-weixin/pages/index/index.wxml',
    content: `<!-- 模拟的页面WXML -->
<view class="container">
  <text>{{message}}</text>
</view>`
  },
  {
    path: 'dist/build/mp-weixin/pages/index/index.wxss',
    content: `/* 模拟的页面样式 */
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}`
  },
  {
    path: 'dist/build/mp-weixin/pages/index/index.json',
    content: JSON.stringify({
      "navigationBarTitleText": "首页"
    }, null, 2)
  }
];

files.forEach(file => {
  fs.writeFileSync(file.path, file.content);
  console.log(`📄 创建文件: ${file.path}`);
});

console.log('✅ 模拟构建完成！');
console.log('📦 生成的文件:');
// 列出所有生成的文件
files.forEach(file => {
  const stat = fs.statSync(file.path);
  console.log(`  ${file.path} (${stat.size} bytes)`);
});
EOF

# 创建Dockerfile
cat > Dockerfile.mock << 'EOF'
FROM --platform=linux/amd64 node:18.18.2-alpine

WORKDIR /app

# 复制项目文件
COPY package.json ./
COPY create-mock-files.js ./

# 执行构建
RUN npm run build:mp-weixin

# 验证构建结果
RUN echo "📁 检查dist目录:"
RUN ls -la dist/build/mp-weixin/
RUN echo "📄 检查生成的文件:"
RUN find dist/build/mp-weixin/ -type f -exec ls -la {} \;

CMD ["tail", "-f", "/dev/null"]
EOF

echo "✅ 创建了模拟构建测试"
echo "🔧 构建测试..."
docker build -f Dockerfile.mock -t mock-build-test .

if [ $? -eq 0 ]; then
    echo "✅ 模拟构建成功！"
    echo "🔍 检查容器内容..."
    docker run --rm mock-build-test sh -c "echo '📁 最终检查:' && ls -la dist/build/mp-weixin/ && echo '📄 文件详情:' && find dist/build/mp-weixin/ -type f"
else
    echo "❌ 模拟构建失败"
fi

# 清理
cd ..
rm -rf mock-build-test
